import { req_addGoods_toShopcar, req_shopCart_goodsList_obj, req_delGoods_inShopcart, req_change_goodsChecked } from '../../api/req_fun'
import { Message } from 'element-ui'
import { Get_uuid_token} from '../../util/My_fun.js'
export default {
    namespaced: true,
    state: () => ({
        uuid_token: Get_uuid_token(),
        shopCart_goodsList_obj: {}
    }),
    actions: {
        async get_shopCart_goodsList_obj({ commit }) {
            const data = await req_shopCart_goodsList_obj()
            if (data.code === 200) {
                commit('GET_SHOPCART_GOODSLIST_OBJ', data.data);
            } else {
                Message.error('请求失败!')
            }
        },
        // 添加商品 修改数量
        async add_shopGoods({ commit }, { skuiD, skuNum }) {
            const data = await req_addGoods_toShopcar({ skuiD, skuNum })
            if (data.code === 200) {
                return 'ok'
            } else {
                return Promise.reject(new Error('file!'))
            } 
        },
        // 从购物车里删除商品
        async del_goods_inShopcart(context, skuiD) {
            const data = await req_delGoods_inShopcart({ skuiD })
            if (data.code === 200) {
                return context.dispatch('get_shopCart_goodsList_obj')
            } else {
                return Promise.reject(new Error('file!'))
            } 
        },
        // 删除所有 已选中的 商品
        async del_All_checked_goods({ dispatch, getters }) {
            let MyPromise_Array = [];
            getters.shopCart_GoodsList.forEach((item) => {
                if (item.isChecked === 1) {
                    const myPromise = dispatch('del_goods_inShopcart', item.skuId)
                    MyPromise_Array.push(myPromise)
                }
            })
            // 判断是否全部成功
            try {
                const val = await Promise.all(MyPromise_Array)
                dispatch('get_shopCart_goodsList_obj')
            } catch {
                Message.error('修改错误!')
            }
        },
        // 修改商品的旋蒸状态
        async change_goodsChecked(context,{skuID, isChecked}) {
            const data = await req_change_goodsChecked({ skuID, isChecked})
            if (data.code === 200) {
                return context.dispatch('get_shopCart_goodsList_obj')
            } else {
                return Promise.reject(new Error('file!'))
            } 
        },
        // 全选按钮状态变化 循环改变所有商品的状态
        async Set_all_goods_checkedStatus({ dispatch, getters }, newStatus) {
            let MyPromise_Array = [];
            getters.shopCart_GoodsList.forEach((item) => {
                const myPromise = dispatch('change_goodsChecked', { skuID: item.skuId, isChecked: newStatus })
                MyPromise_Array.push(myPromise)
            })
            // 判断是否全部成功
            try {
                const val = await Promise.all(MyPromise_Array)
                dispatch('get_shopCart_goodsList_obj')
            } catch {
                Message.error('修改错误!')
            }
        }
    },
    mutations: {
        GET_SHOPCART_GOODSLIST_OBJ(state, data) {
            state.shopCart_goodsList_obj = data
        }
    },
    getters: {
        shopCart_GoodsList(state) {
            return state.shopCart_goodsList_obj[0]? state.shopCart_goodsList_obj[0].cartInfoList : []
        },
        active_sum_Goods(state) {
            let isHas = state.shopCart_goodsList_obj[0]? true : false
            if (isHas) {
                return state.shopCart_goodsList_obj[0].cartInfoList.reduce((before, item) => {
                    return item.isChecked === 1? before+=item.skuNum : before+=0
                }, 0)
            } else {
                return 0
            }
        },
        active_sum_price(state) {
            let isHas = state.shopCart_goodsList_obj[0]? true : false
            if (isHas) {
                return state.shopCart_goodsList_obj[0].cartInfoList.reduce((before, item) => {
                    return item.isChecked === 1? before+=(item.skuNum*item.skuPrice) : before+=0
                }, 0)
            } else {
                return 0
            }
        },
        Is_All_active(state) {
            let isHas = state.shopCart_goodsList_obj[0]? true : false
            if (isHas) {
                return state.shopCart_goodsList_obj[0].cartInfoList.every((item) => {
                    return item.isChecked === 1
                })
            } else {
                return false
            }
        },
    }
}